$(function() {

    var basicInfoDiv = $('#basic-info'),

        educationDiv = $('#education'),
        educationAddBtn = educationDiv.find('a'),

        workDiv = $('#work-experience'),
        workAddBtn = workDiv.find('a'),

        projectDiv = $('#project-experience'),
        projectAddBtn = projectDiv.find('a'),

        selfDiv = $('#self-desc'),

        skillDiv = $('#skill'),
        skillAddBtn = skillDiv.find('a'),

        disireDiv = $('#disire'),

        previewBtn = $('#preview'),
        exportBtn = $('#export'),

        msgModal = $('#msg-modal'),
        confirmModal = $('#confirm-modal'),
        tplModal = $('#tpl-modal');

    var dataStorage = {
        resume: {
            '#basic-info': undefined,
            '#education': {},
            '#work-experience': {},
            '#project-experience': {},
            '#self-desc': undefined,
            '#skill': {},
            '#disire': undefined,
        },
        tpl: 1
    };

    $(".date-birthday").datepicker({
        format: 'yyyy.mm',
        viewMode: 'years',
        minViewMode: 'months'
    }); 

    $('#module-switch').find('a').on('click', function(e) {
        e.preventDefault();
        var self = $(this);
        $.each(self.siblings(), function(i, v) {
            var v = $(v);
            $(v.data('module')).hide();
            v.removeClass('active');
        });
        self.addClass('active');

        var moduleId = self.data('module'),
        	targetModule = $(moduleId);

        if (moduleId == '#basic-info') {
            basicInfoDiv.find('input[type="text"]:eq(0)').val(dataStorage.resume[moduleId].name);
            basicInfoDiv.find('select:eq(0)').val(dataStorage.resume[moduleId].sex);
            basicInfoDiv.find('img').attr('src', MODELS.DOMAIN_PATH + dataStorage.resume[moduleId].headpic);
            basicInfoDiv.find('input[type="text"]:eq(1)').val(dataStorage.resume[moduleId].birthday);
            basicInfoDiv.find('input[type="text"]:eq(2)').val(dataStorage.resume[moduleId].school);
            basicInfoDiv.find('input[type="text"]:eq(3)').val(dataStorage.resume[moduleId].major);
            basicInfoDiv.find('select:eq(1)').val(dataStorage.resume[moduleId].degree);
            basicInfoDiv.find('select:eq(2)').val(dataStorage.resume[moduleId].workage);
            basicInfoDiv.find('input[type="text"]:eq(4)').val(dataStorage.resume[moduleId].phone);
            basicInfoDiv.find('input[type="text"]:eq(5)').val(dataStorage.resume[moduleId].mail);
            basicInfoDiv.find('input[type="text"]:eq(6)').val(dataStorage.resume[moduleId].city);
        } else if (moduleId == '#self-desc') {
            targetModule.empty();
            targetModule.append(constructSelfDiv(dataStorage.resume[moduleId]));
        } else if (moduleId == '#disire') {
            targetModule.empty();
            targetModule.append(constructDisireDiv(dataStorage.resume[moduleId]));
        } else {
            targetModule.find('.panel').each(function(i, v) {
                $(v).parent().remove();
            });
            if (moduleId == '#work-experience') {
                for (var key in dataStorage.resume[moduleId]) {
                    targetModule.prepend(constructWorkDiv(dataStorage.resume[moduleId][key]));
                }
            } else if (moduleId == '#project-experience') {
                for (var key in dataStorage.resume[moduleId]) {
                    targetModule.prepend(constructProjectDiv(dataStorage.resume[moduleId][key]));
                }
            } else if (moduleId == "#education") {
                for (var key in dataStorage.resume[moduleId]) {
                    targetModule.prepend(constructEducationDiv(dataStorage.resume[moduleId][key]));
                }
            } else if (moduleId == "#skill") {
                for (var key in dataStorage.resume[moduleId]) {
                    targetModule.prepend(constructSkillDiv(dataStorage.resume[moduleId][key]));
                }
            }
        }
        $(self.data('module')).show();

    });

    $.get(MODELS.DOMAIN_PATH + '/resume/seeker/' + MODELS.ID, function(resume) {
    	// Set basic info
    	basicInfoDiv.find('input[type="text"]:eq(0)').val(resume.seeker.name);
    	basicInfoDiv.find('select:eq(0)').val(resume.seeker.sex);
    	basicInfoDiv.find('img').attr('src', MODELS.DOMAIN_PATH + resume.seeker.headpic);
    	basicInfoDiv.find('input[type="text"]:eq(1)').val(resume.seeker.birthday);
    	basicInfoDiv.find('input[type="text"]:eq(2)').val(resume.seeker.school);
    	basicInfoDiv.find('input[type="text"]:eq(3)').val(resume.seeker.major);
    	basicInfoDiv.find('select:eq(1)').val(resume.seeker.degree);
    	basicInfoDiv.find('select:eq(2)').val(resume.seeker.workage);
    	basicInfoDiv.find('input[type="text"]:eq(4)').val(resume.seeker.phone);
    	basicInfoDiv.find('input[type="text"]:eq(5)').val(resume.seeker.mail);
    	basicInfoDiv.find('input[type="text"]:eq(6)').val(resume.seeker.city);

        dataStorage.resume['#basic-info'] = resume.seeker;

    	// Set education
    	resume.educationLst.forEach(function(education) {
    		educationAddBtn.parent().before(constructEducationDiv(education));
    	});
    	// Set work experience
    	resume.workExperienceLst.forEach(function(work) {
    		workAddBtn.parent().before(constructWorkDiv(work));
    	});
    	// Set project experience
    	resume.projectExperienceLst.forEach(function(project) {
    		projectAddBtn.parent().before(constructProjectDiv(project));
    	});
    	// Set self desc 
    	selfDiv.append(constructSelfDiv(resume.selfDescLst[0]));
    	// set skill
    	resume.skillLst.forEach(function(skill) {
    		skillAddBtn.parent().before(constructSkillDiv(skill));
    	});
    	// Set disire
    	disireDiv.append(constructDisireDiv(resume.disireLst[0]));
    });

    basicInfoDiv.find('input[type="file"]').on('change', function(e) {
        var img = $(this)[0].files[0];
        if (!img) return;
        var postData = new FormData();
        postData.append('img', $(this)[0].files[0]);
        $.ajax({
            url: MODELS.DOMAIN_PATH + '/seeker/id/' + MODELS.ID + '/img',
            data: postData,
            type: 'POST',
            cache: false,
            contentType: false,
            processData: false
        }).done(function(result) {
            if (result.succeeded) {
                basicInfoDiv.find('img').attr('src', MODELS.DOMAIN_PATH + result.message);
            }
        });
    });

    basicInfoDiv.find('button').on('click', function(e) {
        var inputDivs = basicInfoDiv.find('input[type="text"]'),
            selectDivs = basicInfoDiv.find('select');
        var param = {
            name: inputDivs[0].value,
            sex: selectDivs[0].value,
            headpic: (function(url) {
                return url.substring(MODELS.DOMAIN_PATH.length, url.length);
            })(basicInfoDiv.find('img').attr('src')),
            birthday: inputDivs[1].value,
            school: inputDivs[2].value,
            major: inputDivs[3].value,
            degree: selectDivs[1].value,
            workage: selectDivs[2].value,
            phone: inputDivs[4].value,
            mail: inputDivs[5].value,
            city: inputDivs[6].value
        };
        for (var key in param) {
            if (param[key].length == 0) {
            	showMsgBox('缺少 ' + key + ' 信息');
                return;
            }
        }

        $.ajax({
            url: MODELS.DOMAIN_PATH + '/seeker/id/' + MODELS.ID,
            data: param,
            type: 'POST'
        }).done(function(result) {
            if (result.succeeded) {
            	showMsgBox('Modify successfully');
                dataStorage.resume['#basic-info'] = result.obj;
            } else {
            	showMsgBoxn('Modify failed: ' + result.message);
            }
        });
    });

    educationAddBtn.on('click', function(e) {
    	e.preventDefault();
    	educationAddBtn.parent().before(constructEducationDiv());
    });

    workAddBtn.on('click', function(e) {
    	e.preventDefault();
    	workAddBtn.parent().before(constructWorkDiv());
    });

    projectAddBtn.on('click', function(e) {
    	e.preventDefault();
    	projectAddBtn.parent().before(constructProjectDiv());
    });

    skillAddBtn.on('click',function(e) {
    	e.preventDefault();
    	skillAddBtn.parent().before(constructSkillDiv());
    })

    function constructEducationDiv(education) {
    	var div = $('<div class="col-md-6" data-id="' + (education ? education.id : 0) + '">' +
    				'<div class="panel panel-default">' +
                        '<div class="panel-body">' +
                            '<form class="form-horizontal">' +
                                '<div class="form-group">' +
                                    '<label class="col-md-4 control-label">学校名称</label>' +
                                    '<div class="col-md-8">' +
                                        '<input class="form-control" type="text" value="' + (education ? education.school : '') + '">' +
                                    '</div>' +
                                '</div>' +
                                '<div class="form-group">' +
                                    '<label class="col-sm-4 control-label">学校类型</label>' +
                                    '<div class="col-sm-8">' +
                                        '<select class="form-control">' +
                                            '<option value="0">985院校</option>' +
                                            '<option value="1">211院校</option>' +
                                            '<option value="2">其他</option>' +
                                        '</select>' +
                                    '</div>' +
                                '</div>' +
                                '<div class="form-group">' +
                                    '<label class="col-sm-4 control-label">主修专业</label>' +
                                    '<div class="col-sm-8">' +
                                        '<input type="text" class="form-control" value="' + (education ? education.major : '') + '">' +
                                    '</div>' +
                                '</div>' +
                                '<div class="form-group">' +
                                    '<label class="col-sm-4 control-label">学历</label>' +
                                    '<div class="col-sm-8">' +
                                        '<select class="form-control">' +
                                            '<option value="0">大专</option>' +
                                            '<option value="1">本科</option>' +
                                            '<option value="2">硕士</option>' +
                                            '<option value="3">博士</option>' +
                                        '</select>' +
                                    '</div>' +
                                '</div>' +
                                '<div class="form-group">' +
                                    '<label class="col-sm-4 control-label">在校时间</label>' +
                                    '<div class="col-sm-8">' +
                                        '<div class="input-group">' +
                                            '<input type="text" class="form-control date-school-duration" placeholder="yyyy" value="">' +
                                                '<span class="input-group-addon">' +
                                                    '<span class="glyphicon glyphicon-menu-right"></span>' +
                                                '</span>' +
                                            '<input type="text" class="form-control date-school-duration" placeholder="yyyy" value="">' +
                                        '</div>' +
                                    '</div>' +
                                '</div>' +
                            '</form>' +
                        '</div>' +
                        '<div class="panel-footer" style="height: 55px;">' +
                            (education ? '<button type="button" class="btn btn-default">删除</button>' : '') +
                            '<button type="button" class="btn btn-primary pull-right">' + (education ? '更新' : '保存') + '</button>' +
                        '</div>' +
                    '</div>' + 
                    '</div>');
    	div.find('select:eq(0)').val(education ? education.type : 0);
    	div.find('select:eq(1)').val(education ? education.degree : 0);

        var durationDiv = div.find('.date-school-duration');
        if (education && education.duration) {
            durationDiv.each(function(i, v) {
                $(v).val(education.duration.split('-')[i]);
            });
        }
        durationDiv.datepicker({
            format: ' yyyy',
            viewMode: 'years',
            minViewMode: 'years'
        });

    	div.find('.btn-default').on('click', rmClickCb);
        
    	div.find('.btn-primary').on('click', function() {
    		var self = $(this);
    		var param = {
    			school: div.find('input[type="text"]:eq(0)').val(),
    			type: div.find('select:eq(0)').val(),
    			major: div.find('input[type="text"]:eq(1)').val(),
    			degree: div.find('select:eq(1)').val(),
    			duration: div.find('input[type="text"]:eq(2)').val() + '-' + div.find('input[type="text"]:eq(3)').val(),
    		};
    		for (var key in param) {
    			if (param[key].length == 0) {
    				showMsgBox('缺少 ' + key + ' 信息');
    				return;
    			}
    		}
    		if (div.data('id') == 0) {
    		    $.post(MODELS.DOMAIN_PATH + '/resume/seeker/' + MODELS.ID + '/education', param, function(result) {
    		        if (result.succeeded) {
    		            showMsgBox('Save successfully');
    		            div.data('id', result.obj.id);
    		            self.text('更新');
    		            var removeBtnDiv = $('<button type="button" class="btn btn-default">删除</button>');
    		            removeBtnDiv.on('click', rmClickCb);
    		            self.before(removeBtnDiv);
                        dataStorage.resume['#education'][result.obj.id] = result.obj;
    		        }
    		    });
    		} else {
    		    $.post(MODELS.DOMAIN_PATH + '/resume/education/id/' + div.data('id'), param, function(result) {
    		        if (result.succeeded) {
    		            showMsgBox('Modify successfully');
                        dataStorage.resume['#education'][div.data('id')] = result.obj;
    		        }
    		    });
    		}
    	});

    	function rmClickCb() {
    		// Confirm
    	    $.ajax({
    	        url: MODELS.DOMAIN_PATH + '/resume/education/id/' + div.data('id'),
    	        type: 'DELETE',
    	    }).done(function(result) {
    	        if (result.succeeded) {
    	            div.remove();
    	        }
    	    });
    	}
        if (education) dataStorage.resume['#education'][education.id] = education;
    	return div;
    }

    function constructWorkDiv(work) {
    	var div = $('<div class="col-md-6" data-id="' + (work ? work.id : 0) + '">' +
	                    '<div class="panel panel-default">' +
	                        '<div class="panel-body">' +
	                            '<form class="form-horizontal">' +
	                                '<div class="form-group">' +
	                                    '<label class="col-md-4 control-label">公司</label>' +
	                                    '<div class="col-md-8">' +
	                                        '<input class="form-control" type="text" value="' + (work ? work.company : '')+ '">' +
	                                    '</div>' +
	                                '</div>' +
	                                '<div class="form-group">' +
	                                    '<label class="col-sm-4 control-label">职位</label>' +
	                                    '<div class="col-sm-8">' +
	                                        '<input class="form-control" type="text" value="' + (work ? work.position : '') + '">' +
	                                    '</div>' +
	                                '</div>' +
	                                '<div class="form-group">' +
	                                    '<label class="col-sm-4 control-label">在职时间</label>' +
	                                    '<div class="col-sm-8">' +
	                                        //'<input type="text" class="form-control" placeholder="yyyy.mm-yyyy.mm" value="' + (work ? work.duration : '') + '">' +
                                            '<div class="input-group">' +
                                                '<input type="text" class="form-control date-work-duration" placeholder="yyyy.mm" value="">' +
                                                    '<span class="input-group-addon">' +
                                                        '<span class="glyphicon glyphicon-menu-right"></span>' +
                                                    '</span>' +
                                                '<input type="text" class="form-control date-work-duration" placeholder="yyyy.mm" value="">' +
                                            '</div>' +
	                                    '</div>' +
	                                '</div>' +
	                                '<div class="form-group">' +
	                                    '<label class="col-sm-4 control-label">工作内容</label>' +
	                                    '<div class="col-sm-8">' +
	                                        '<textarea class="form-control" rows="5">' + (work ? work.task : '') + '</textarea>' +
	                                    '</div>' +
	                                '</div>' +
	                            '</form>' +
	                        '</div>' +
	                        '<div class="panel-footer" style="height: 55px;">' +
	                            (work ? '<button type="button" class="btn btn-default">删除</button>' : '')+
	                            '<button type="button" class="btn btn-primary pull-right">' + (work ? '更新' : '保存') + '</button>' +
	                        '</div>' +
	                    '</div>' +
	                '</div>');

        var durationDiv = div.find('.date-work-duration');
        if (work && work.duration) {
            durationDiv.each(function(i, v) {
                $(v).val(work.duration.split('-')[i]);
            });
        }

        durationDiv.datepicker({
            format: 'yyyy.mm',
            viewMode: 'years',
            minViewMode: 'months'
        });

    	div.find('.btn-default').on('click', rmClickCb);
    	div.find('.btn-primary').on('click', function() {
    		var self = $(this);
    		var param = {
    			company: div.find('input[type="text"]:eq(0)').val(),
    			position: div.find('input[type="text"]:eq(1)').val(),
    			duration: div.find('input[type="text"]:eq(2)').val() + '-' + div.find('input[type="text"]:eq(3)').val(),
    			task: div.find('textarea').val()
    		}
    		for (var key in param) {
    			if (param[key].length == 0) {
    				showMsgBox('缺少 ' + key + ' 信息');
    				return;
    			}
    		}
    		if (div.data('id') == 0) {
    			$.post(MODELS.DOMAIN_PATH + '/resume/seeker/' + MODELS.ID + '/work', param, function(result) {
    				if (result.succeeded) {
    					showMsgBox('Save successfully');
    		            div.data('id', result.obj.id);
    		            self.text('更新');
    		            var removeBtnDiv = $('<button type="button" class="btn btn-default">删除</button>');
    		            removeBtnDiv.on('click', rmClickCb);
    		            self.before(removeBtnDiv);
                        dataStorage.resume['#work-experience'][result.obj.id] = result.obj;
    				}
    			});
    		} else {
    			$.post(MODELS.DOMAIN_PATH + '/resume/work/id/' + div.data('id'), param, function(result) {
    		        if (result.succeeded) {
    		            showMsgBox('Modify successfully');
                        dataStorage.resume['#work-experience'][div.data('id')] = result.obj;
    		        }
    		    });
    		}

    	});
    	function rmClickCb() {
    		$.ajax({
    			url: MODELS.DOMAIN_PATH + '/resume/work/id/' + div.data('id'),
    			type: 'DELETE'
    		}).done(function(result) {
    			if (result.succeeded) {
    				div.remove();
    			}
    		});
    	}
        if (work) dataStorage.resume['#work-experience'][work.id] = work;
    	return div;
    }

    function constructProjectDiv(project) {
    	var div = $('<div class="col-md-6" data-id="' + (project ? project.id : 0) + '">' +
	                    '<div class="panel panel-default">' +
	                        '<div class="panel-body">' +
	                            '<form class="form-horizontal">' +
	                                '<div class="form-group">' +
	                                    '<label class="col-md-4 control-label">项目名称</label>' +
	                                    '<div class="col-md-8">' +
	                                        '<input class="form-control" type="text" value="' + (project ? project.proName : '') + '">' +
	                                    '</div>' +
	                                '</div>' +
	                                '<div class="form-group">' +
	                                    '<label class="col-sm-4 control-label">职责</label>' +
	                                    '<div class="col-sm-8">' +
	                                        '<input class="form-control" type="text" value="' + (project ? project.position : '') + '">' +
	                                    '</div>' +
	                                '</div>' +
	                                '<div class="form-group">' +
	                                    '<label class="col-sm-4 control-label">项目时间</label>' +
	                                    '<div class="col-sm-8">' +
	                                        //'<input type="text" class="form-control" placeholder="yyyy.mm-yyyy.mm" value="' + (project ? project.duration : '') + '">' +
                                            '<div class="input-group">' +
                                                '<input type="text" class="form-control date-project-duration" placeholder="yyyy.mm" value="">' +
                                                    '<span class="input-group-addon">' +
                                                        '<span class="glyphicon glyphicon-menu-right"></span>' +
                                                    '</span>' +
                                                '<input type="text" class="form-control date-project-duration" placeholder="yyyy.mm" value="">' +
                                            '</div>' +
	                                    '</div>' +
	                                '</div>' +
	                                '<div class="form-group">' +
	                                    '<label class="col-sm-4 control-label">项目描述</label>' +
	                                    '<div class="col-sm-8">' +
	                                        '<textarea class="form-control" rows="5">' + (project ? project.proDesc : '') + '</textarea>' +
	                                    '</div>' +
	                                '</div>' +
	                            '</form>' +
	                        '</div>' +
	                        '<div class="panel-footer" style="height: 55px;">' +
	                            (project ? '<button type="button" class="btn btn-default">删除</button>' : '') +
	                            '<button type="button" class="btn btn-primary pull-right">' + (project ? '更新' : '保存') + '</button>' +
	                        '</div>' +
	                    '</div>' +
	                '</div>');

        var durationDiv = div.find('.date-project-duration');
        if (project && project.duration) {
            durationDiv.each(function(i, v) {
                $(v).val(project.duration.split('-')[i]);
            });
        }

        durationDiv.datepicker({
            format: 'yyyy.mm',
            viewMode: 'years',
            minViewMode: 'months'
        });

    	div.find('.btn-default').on('click', rmClickCb);
    	div.find('.btn-primary').on('click', function() {
    		var self = $(this);
    		var param = {
    			proName: div.find('input[type="text"]:eq(0)').val(),
    			position: div.find('input[type="text"]:eq(1)').val(),
    			duration: div.find('input[type="text"]:eq(2)').val() + '-' +div.find('input[type="text"]:eq(3)').val(),
    			proDesc: div.find('textarea').val()
    		}
    		for (var key in param) {
    			if (param[key].length == 0) {
    				showMsgBox('缺少 ' + key + ' 信息');
    				return;
    			}
    		}
    		if (div.data('id') == 0) {
    			$.post(MODELS.DOMAIN_PATH + '/resume/seeker/' + MODELS.ID + '/project', param, function(result) {
    				if (result.succeeded) {
    					showMsgBox('Save successfully');
    		            div.data('id', result.obj.id);
    		            self.text('更新');
    		            var removeBtnDiv = $('<button type="button" class="btn btn-default">删除</button>');
    		            removeBtnDiv.on('click', rmClickCb);
    		            self.before(removeBtnDiv);
                        dataStorage.project['#project-experience'][result.obj.id] = result.obj;
    				}
    			});
    		} else {
    			$.post(MODELS.DOMAIN_PATH + '/resume/project/id/' + div.data('id'), param, function(result) {
    		        if (result.succeeded) {
    		            showMsgBox('Modify successfully');
                        dataStorage.project['#project-experience'][div.data('id')] = result.obj;
    		        }
    		    });
    		}

    	});
    	function rmClickCb() {
    		$.ajax({
    			url: MODELS.DOMAIN_PATH + '/resume/project/id/' + div.data('id'),
    			type: 'DELETE'
    		}).done(function(result) {
    			if (result.succeeded) {
    				div.remove();
    			}
    		});
    	}
        if (project) dataStorage.resume['#project-experience'][project.id] = project;
    	return div;
    }

    function constructSelfDiv(self) {
    	var div = $('<div class="panel panel-default" data-id="' + (self ? self.id : 0) + '">' +
	                    '<div class="panel-body">' +
	                        '<form>' +
	                            '<div class="form-group">' +
	                                '<label>自我描述</label>' +
	                                '<textarea class="form-control" rows="10">' + (self ? self.content : '') + '</textarea>' +
	                           ' </div>' +
	                        '</form>' +
	                    '</div>' +
	                    '<div class="panel-footer" style="height: 55px;">' +
	                        '<button type="button" class="btn btn-primary pull-right">' + (self ? '更新' : '保存') + '</button>' +
	                    '</div>' +
	                '</div>');

    	div.find('button').on('click', function() {
    		var self = $(this);
    		var param = {
    			content: div.find('textarea').val()
    		}
    		for (var key in param) {
    			if (param[key].length == 0) {
    				showMsgBox('缺少 ' + key + ' 信息');
    				return;
    			}
    		}
			if (div.data('id') == 0) {
    			$.post(MODELS.DOMAIN_PATH + '/resume/seeker/' + MODELS.ID + '/self', param, function(result) {
    				if (result.succeeded) {
    					showMsgBox('Save successfully');
    		            div.data('id', result.obj.id);
    		            self.text('更新');
                        dataStorage['#self-desc'] = result.obj;
    				}
    			});
    		} else {
    			$.post(MODELS.DOMAIN_PATH + '/resume/self/id/' + div.data('id'), param, function(result) {
    		        if (result.succeeded) {
                        dataStorage['#self-desc'] = result.obj;
    		            showMsgBox('Modify successfully');
    		        }
    		    });
    		}
    	});
        if (self) dataStorage.resume['#self-desc'] = self;
    	return div;
    }

    function constructSkillDiv(skill) {
    	var div = $('<div class="col-md-6" data-id="' + (skill ? skill.id : 0) + '">' +
	                    '<div class="panel panel-default">' +
	                        '<div class="panel-body">' +
	                            '<form class="form-horizontal">' +
	                                '<div class="form-group">' +
	                                    '<label class="col-md-4 control-label">技能名称</label>' +
	                                    '<div class="col-md-8">' +
	                                        '<input class="form-control" type="text" value="' + (skill ? skill.name : '') + '">' +
	                                    '</div>' +
	                                '</div>' +
	                                '<div class="form-group">' +
	                                    '<label class="col-sm-4 control-label">熟练程度</label>' +
	                                    '<div class="col-sm-8">' +
	                                        '<select class="form-control">' +
	                                            '<option value="0">了解</option>' +
	                                            '<option value="1">熟悉</option>' +
	                                            '<option value="2">掌握</option>' +
	                                            '<option value="3">精通</option>' +
	                                            '<option value="4">专家</option>' +
	                                        '</select>' +
	                                    '</div>' +
	                                '</div>' +
	                            '</form>' +
	                        '</div>' +
	                        '<div class="panel-footer" style="height: 55px;">' +
	                            (skill ? '<button type="button" class="btn btn-default">删除</button>' : '') +
	                            '<button type="button" class="btn btn-primary pull-right">' + (skill ? '更新' : '保存') + '</button>' +
	                        '</div>' +
	                    '</div>' +
	                '</div>');

    	div.find('select').val(skill ? skill.level : 0);

    	div.find('.btn-default').on('click', rmClickCb);
    	div.find('.btn-primary').on('click', function() {
    		var self = $(this);
    		var param = {
    			name: div.find('input[type="text"]').val(),
    			level: div.find('select').val(),
    		}
    		for (var key in param) {
    			if (param[key].length == 0) {
    				showMsgBox('缺少 ' + key + ' 信息');
    				return;
    			}
    		}
    		if (div.data('id') == 0) {
    			$.post(MODELS.DOMAIN_PATH + '/resume/seeker/' + MODELS.ID + '/skill', param, function(result) {
    				if (result.succeeded) {
    					showMsgBox('Save successfully');
    		            div.data('id', result.obj.id);
    		            self.text('更新');
    		            var removeBtnDiv = $('<button type="button" class="btn btn-default">删除</button>');
    		            removeBtnDiv.on('click', rmClickCb);
    		            self.before(removeBtnDiv);
                        dataStorage.resume['#skill'][result.obj.id] = result.obj;
    				}
    			});
    		} else {
    			$.post(MODELS.DOMAIN_PATH + '/resume/skill/id/' + div.data('id'), param, function(result) {
    		        if (result.succeeded) {
    		            showMsgBox('Modify successfully');
                        dataStorage.resume['#skill'][div.data('id')] = result.obj;
    		        }
    		    });
    		}

    	});
    	function rmClickCb() {
    		$.ajax({
    			url: MODELS.DOMAIN_PATH + '/resume/skill/id/' + div.data('id'),
    			type: 'DELETE'
    		}).done(function(result) {
    			if (result.succeeded) {
    				div.remove();
    			}
    		});
    	}
        if (skill) dataStorage.resume['#skill'][skill.id] = skill;
    	return div;
    }

    function constructDisireDiv(disire) {
    	var div = $('<div class="panel panel-default" data-id="' + (disire ? disire.id : 0) + '">' +
	                    '<div class="panel-body">' +
	                        '<form class="form-horizontal">' +
	                            '<div class="form-group">' +
	                                '<label class="col-md-2 control-label">期望岗位</label>' +
	                                '<div class="col-md-10">' +
	                                    '<input class="form-control" type="text" value="' + (disire ? disire.position : '') + '">' +
	                                '</div>' +
	                            '</div>' +
	                            '<div class="form-group">' +
	                                '<label class="col-sm-2 control-label">期望城市</label>' +
	                                '<div class="col-sm-10">' +
	                                    '<input class="form-control" type="text" value="' + (disire ? disire.city : '') + '">' +
	                                '</div>' +
	                            '</div>' +
	                            '<div class="form-group">' +
	                                '<label class="col-sm-2 control-label">在岗类型</label>' +
	                                '<div class="col-sm-10">' +
	                                    '<select class="form-control">' +
	                                        '<option value="0">全职</option>' +
	                                        '<option value="1">兼职</option>' +
	                                        '<option value="2">实习</option>' +
	                                    '</select>' +
	                                '</div>' +
	                            '</div>' +
	                            '<div class="form-group">' +
	                                '<label class="col-sm-2 control-label">薪资</label>' +
	                                '<div class="col-sm-10">' +
	                                    '<select class="form-control">' +
	                                        '<option value="0">2K 以下</option>' +
	                                        '<option value="1">2K-5K</option>' +
	                                        '<option value="2">5K-10K</option>' +
	                                        '<option value="3">10K-15K</option>' +
	                                        '<option value="4">15K-20K</option>' +
	                                        '<option value="5">20K-25K</option>' +
	                                        '<option value="6">25K-50K</option>' +
	                                        '<option value="7">50K 以上</option>' +
	                                    '</select>' +
	                                '</div>' +
	                            '</div>' +
	                            '<div class="form-group">' +
	                                '<label class="col-sm-2 control-label">到岗日期</label>' +
	                                '<div class="col-sm-10">' +
	                                    '<input type="text" class="form-control date-disire" placeholder="yyyy-mm-dd" value="' + (disire ? disire.intentDate : '') + '">' +
	                                '</div>' +
	                            '</div>' +
	                        '</form>' +
	                    '</div>' +
	                    '<div class="panel-footer" style="height: 55px;">' +
	                        '<button type="button" class="btn btn-primary pull-right">' + (disire ? '更新' : '保存') + '</button>' +
	                    '</div>' +
	                '</div>');

        div.find('.date-disire').datepicker({
            format: 'yyyy.mm.dd',
        });

    	div.find('select:eq(0)').val(disire ? disire.type : 0);
    	div.find('select:eq(1)').val(disire ? disire.salary : 0);

    	div.find('button').on('click', function() {
    		var self = $(this);
    		var param = {
    			position: div.find('input[type="text"]:eq(0)').val(),
    			city: div.find('input[type="text"]:eq(1)').val(),
    			type: div.find('select:eq(0)').val(),
    			salary: div.find('select:eq(1)').val(),
    			intentDate: div.find('input[type="text"]:eq(2)').val()
    		}
    		for (var key in param) {
    			if (param[key].length == 0) {
    				showMsgBox('缺少 ' + key + ' 信息');
    				return;
    			}
    		}
			if (div.data('id') == 0) {
    			$.post(MODELS.DOMAIN_PATH + '/resume/seeker/' + MODELS.ID + '/disire', param, function(result) {
    				if (result.succeeded) {
    					showMsgBox('Save successfully');
    		            div.data('id', result.obj.id);
    		            self.text('更新');
                        dataStorage.resume['#disire'] = result.obj;
    				}
    			});
    		} else {
    			$.post(MODELS.DOMAIN_PATH + '/resume/disire/id/' + div.data('id'), param, function(result) {
    		        if (result.succeeded) {
    		            showMsgBox('Modify successfully');
                        dataStorage.resume['#disire'] = result.obj;
    		        }
    		    });
    		}

    	});
        if (disire) dataStorage.resume['#disire'] = disire;
    	return div;
    }

    function showMsgBox(msg) {
    	msgModal.find('.modal-body').text(msg);
    	msgModal.modal('show');
    }

    tplModal.on('show.bs.modal', function() {
        var countDiv = tplModal.find('.glyphicon-thumbs-up');
        $.get(MODELS.DOMAIN_PATH + '/templates', function(result) {
            result.forEach(function(v, i) {
                if (i == 5) return;
                $(countDiv[i]).text(v.count);
            });
        });
    });

    $('#tpl-switch').on('click', function(e) {
        e.preventDefault();
        tplModal.modal('show');
    });

});
